package com.dsteiner.xelavos.server;
import static com.dsteiner.xelavos.client.controller.IHistoryToken.HT_OBJECT_ID;
import static com.dsteiner.xelavos.client.controller.IHistoryToken.HT_SELECTED_TAB;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.dsteiner.widgets.client.StringUtil;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.controller.PlaceController;
import com.dsteiner.xelavos.client.domain.Comment;
import com.dsteiner.xelavos.client.domain.HasImage;
import com.dsteiner.xelavos.client.domain.Job;
import com.dsteiner.xelavos.client.domain.Leisure;
import com.dsteiner.xelavos.client.domain.Product;
import com.dsteiner.xelavos.client.domain.Service;
import com.dsteiner.xelavos.client.domain.TrackComments;
import com.dsteiner.xelavos.client.model.MainTabModel;
import com.google.appengine.api.datastore.Text;

public class EmailFormatter {

	public static void format(TrackComments trackComments, Comment comment, ModelObject modelObject, 
			StringBuffer plainTextSb, StringBuffer htmlTextSb) {
		plainTextSb.append("el comentario era: "+comment.getMessage()+"\npara el siguiente registro\n");
		htmlTextSb.append("el comentario era: "+comment.getMessage()+"<br>para el siguiente registro<br>");
		format(modelObject, plainTextSb, htmlTextSb);
		String deleteTrackCommentsUrl = "http://www.xelavos.com/xelavos/emailAdmin?remove="
			+trackComments.getClass().getName()+"&id="+trackComments.getId();
		plainTextSb.append("\npara ya no recibir correos sobre nuevos comentarios visite el siguiente link\n").append(deleteTrackCommentsUrl);
		htmlTextSb.append("<br>para ya no recibir correos sobre nuevos comentarios <a href=\""+deleteTrackCommentsUrl+"\">haga click en ese link</a>");
	}
	
	public static void format(ModelObject modelObject, StringBuffer plainTextSb, StringBuffer htmlTextSb) {
		htmlTextSb.append("<div style=\"border:1px solid black;display:inline-block;margin:5px;vertical-align:top;width:300px;\">");
		if(modelObject instanceof HasImage){
			String imageUrl = Constants.Util.getImageUrl(((HasImage)modelObject).getImageId());
			if(imageUrl != null && !imageUrl.startsWith("http://"))
				imageUrl = "http://www.xelavos.com"+imageUrl;
//			plainTextSb.append(imageUrl).append("\n");
			htmlTextSb.append("<img src=\""+imageUrl+"\"><br>");
		}
		
		StringBuffer infoSb = new StringBuffer();
		String spanishDateFormat = com.dsteiner.widgets.client.constants.Constants.SPANISH_DATE_FORMAT_LONG;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(spanishDateFormat, new Locale("es","ES",""));
		if(modelObject instanceof Product){
			Product product = (Product) modelObject;
			infoSb.append("Nombre : "+product.getName()).append("\n");
			if(product.getPrice() != null){
				DecimalFormat decimalFormat = new DecimalFormat(Constants.NUMBER_FORMAT_QUETZAL);
				infoSb.append("Precio : "+decimalFormat.format(product.getPrice())).append("\n");
			}
			if(product.getShop() != null && !product.getShop().equals(""))
				infoSb.append("Tienda : "+product.getShop()).append("\n");
			if(product.getBrand() != null)
				infoSb.append("Marca : "+product.getBrand()).append("\n");
			if(product.getModel() != null)
				infoSb.append("Modelo : "+product.getModel()).append("\n");
			if(product.getValidUntilDate() != null) {
				infoSb.append("Valido hasta : ").append(simpleDateFormat
				.format(product.getValidUntilDate())).append("\n");
			}
			String contact = product.getContact();
			if(!StringUtil.isEmpty(contact)){
				infoSb.append("Contacto : "+contact).append("\n");
			}
			Text description = product.getDescription();
			if(description != null && !StringUtil.isEmpty(description.toString())){
				infoSb.append("Descripcion : "+StringUtil.toText(description.getValue())).append("\n");
			}
		}
		else if(modelObject instanceof Job){
			Job job= (Job) modelObject;
			infoSb.append("Nombre : "+job.getName()).append("\n");
			if(!StringUtil.isEmpty(job.getEmployer()))
				infoSb.append("Empleador : "+job.getEmployer()).append("\n");
			String contact = job.getContact();
			infoSb.append("Contacto: "+contact).append("\n");
			if(job.getValidUntilDate() != null)
				infoSb.append("valido hasta : ").append(simpleDateFormat.
						format(job.getValidUntilDate())).append("\n");
			Text description = job.getDescription();
			if(description != null && !StringUtil.isEmpty(description.toString())){
				infoSb.append("Descripcion : "+StringUtil.toText(description.getValue()));
			}
		}
		else if(modelObject instanceof Leisure){
			Leisure leisure= (Leisure) modelObject;
			infoSb.append("Nombre : "+leisure.getName()).append("\n");
			infoSb.append("Lugar : "+leisure.getPlace()).append("\n");
			if(leisure.getEventDate() != null){
				SimpleDateFormat shortSimpleDateFormat = new SimpleDateFormat(Constants.DATE_FORMAT, new Locale("es", "ES"));
				try{
					Date date = shortSimpleDateFormat.parse(leisure.getEventDate());
					infoSb.append("Fecha : ").append(simpleDateFormat.format(date)).append("\n");
				}catch(Exception ex){
				}
			}
			if(leisure.getEventHour() != null){
				infoSb.append("Hora : "+
						leisure.getEventHour()).append("\n");
			}
			String contact = leisure.getContact();
			if(contact != null){
				infoSb.append("Contacto : ").append(contact).append("\n");
			}
			Text description = leisure.getDescription();
			if(description != null && !StringUtil.isEmpty(description.toString())){
				infoSb.append("Descripcion : "+StringUtil.toText(description.getValue()));
			}
		}
		else if(modelObject instanceof Service){
			Service service = (Service) modelObject;
			infoSb.append("Tipo : "+service.getType()).append("\n");
			if(service.getName() != null && !service.getName().equals(""))
				infoSb.append("Nombre : "+service.getName()).append("\n");
			if(service.getPlace() != null && !service.getPlace().equals(""))
				infoSb.append("Lugar : "+service.getPlace()).append("\n");
			String contact = service.getContact();
			infoSb.append("Contacto : ").append(contact).append("\n");
			Text description = service.getDescription();
			if(description != null && !StringUtil.isEmpty(description.toString())){
				infoSb.append("Descripcion : "+StringUtil.toText(description.getValue()));
			}
		}
		
		htmlTextSb.append(StringUtil.toHtml(infoSb.toString()));
		plainTextSb.append(infoSb.toString());
		String modelObjectUrl = Constants.Util.getModelObjectUrl(modelObject);

		plainTextSb.append("ver el registro en Xelavos.com bajo el siguiente link ").append(modelObjectUrl);
		htmlTextSb.append("<a href=\""+modelObjectUrl+"\">ver el registro en XelaVos.com</a>");
		
		plainTextSb.append("\n\n");
		htmlTextSb.append("</div><br><br>");
	}

}
